package Persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Vector;

import Negocio.ItemMineral;


public class AdmPersistenciaItemMineral extends AdministradorPersistencia{

	private static AdmPersistenciaItemMineral instancia;
	
	public static AdmPersistenciaItemMineral getInstancia() {
		if (instancia == null)
			instancia = new AdmPersistenciaItemMineral();
		return instancia;
	}
	
	@Override
	public void borrar(Object o) {
		try{
			System.out.println("Entro al instert de Persistencia ItemMineral");
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("delete from itemMineral where codigo = ? ");
			s.setLong(1,(Long)o);
			s.execute();
		}catch(Exception e){
			//TODO Exception
			System.out.println("No se pudo eliminiar el item");
		}
		
	}
	
	@Override
	public void modificar(Object o) {
		// TODO Hacer funcionalidad (ver)
		
	}

	@Override
	public Long insertar(Object o) {
		
			System.out.println("Entro al instert de Persistencia ItemMineral");
			ItemMineral im = (ItemMineral)o;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s;
			try {
				s = con.prepareStatement("insert into ItemMineral values (?,?,?,?)");
			
			s.setLong(1,im.getMineral().getCodigo());
			s.setDouble(2,im.getPorcentaje());
			s.setLong(3,im.getNroLote());
			s.setString(4,im.getTipo());
			s.execute();
			s = con.prepareStatement("select max(codigo) from ItemMineral");
			ResultSet result = s.executeQuery();
			Long res = null;
			if (result.next())
				res = result.getLong(1);
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return res;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//TODO Exception
			System.out.println("No se pudo insertar el elemento");
			return null;
		
		
	}

	@Override
	@Deprecated
	public Vector<Object> buscarTodos() {
		//TODO hacer implementacion
		/*
		System.out.println("Entro a select de item mineral");
		Vector<Object> respuesta = null;
		try{
			ItemMineral a = (ItemMineral)o;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select * from ItemMineral where codigo = ?");
			s.setLong (1,a.getNroItem());
			ResultSet rta = s.executeQuery();
			if(rta.next()){	
				Mineral m = new Mineral();
				m.setCodigo(rta.getLong(2));
				a.setMineral(((Mineral)AdmPersistenciaMineral.getInstancia().select(m).get(0)).getView());
				a.setPorcentaje(rta.getDouble(3));
				a.setNroLote(rta.getLong(4));
			}
			respuesta = new Vector <Object>();
			respuesta.add(a);
		}catch(Exception e){
			System.out.println(e.getStackTrace());
		}
		return respuesta;*/
		return null;
	}


	

	
	@Override
	public Object buscarPorCodigo(Object o) {

		System.out.println("Entro a select de item mineral");
		try{
			ItemMineral i = null;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select * from ItemMineral where codigo = ?");
			s.setLong (1,(Long)o);
			ResultSet rta = s.executeQuery();
			if(rta.next()){	
				i = new ItemMineral (	rta.getLong(1),
										rta.getLong(2),
										rta.getDouble(3),
										rta.getLong(4),
										rta.getString(5)
						
				);
				
			}
			return i;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public Collection<ItemMineral> obtenerPorLote (Long nro){
		

		System.out.println("Entro a select de item mineral");
		try{
			Collection <ItemMineral> res = new Vector<ItemMineral>();
			ItemMineral i = null;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select * from ItemMineral where lote = ?");
			s.setLong (1,(Long)nro);
			ResultSet rta = s.executeQuery();
			while(rta.next()){	
				i = new ItemMineral (	rta.getLong(1),
										rta.getLong(2),
										rta.getDouble(3),
										rta.getLong(4),
										rta.getString(5)
						
				);
				res.add(i);
			}
			return res;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
		
	}
	
	public void eliminarReferencia(long nro) {
		try{
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("delete from ItemMineral where lote = ?");
			s.setLong (1,nro);
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	public void asignarLote(long nro, ItemMineral m) {
	
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s;
			try {
				s = con.prepareStatement("update ItemMineral set lote = ? where codigo = ?");
			
			s.setLong (1,nro);
			s.setLong(2,m.getNroItem());
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}
	
	/*

	public Vector<Object> buscarReferencia(Long nroLote) {
		Vector<Object> respuesta = null;
		try{
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select * from ItemMineral where codigo_lote = ?");
			s.setLong (1,nroLote);
			ResultSet rta = s.executeQuery();
			respuesta = new Vector <Object>();
			while(rta.next()){	
				respuesta.add(new ItemMineral(rta.getLong(1),rta.getLong(2),rta.getDouble(3),rta.getLong(4)));
			}
		}catch(Exception e){
			System.out.println(e.getStackTrace());
		}
		return respuesta;
	}
	*/
}
